<?
include_once "configuration.php";

function quote_smart($value)
{
   if (get_magic_quotes_gpc()) $value = stripslashes($value);
   if (!is_numeric($value)) return addslashes($value);
   return $value;
}

class DB {
	function DB() {
		global $configuration;
		$this->connection = mysql_connect(
					$configuration->get("db_host"),
					$configuration->get("db_user"),
					$configuration->get("db_pwd"));
		$this->prefix = $configuration->get("db_prefix");
		mysql_select_db($configuration->get("db_name"), $this->connection);
		$this->dbg = 0;
		if ((@mysql_num_rows(@mysql_query("SHOW TABLES LIKE '".$this->prefix."_users'"))!=1)) {
			$this->createDB();
		}
	}
	function createDB() {
		$SQLStrings = array(
			"drop table ".$this->prefix."_users",
			"CREATE TABLE ".$this->prefix."_users (
				ID INTEGER AUTO_INCREMENT,
				PRIMARY KEY (ID),
				name TEXT,
				email TEXT,
				password TEXT
			)",
			"drop table ".$this->prefix."_user_attributes",
			"CREATE TABLE ".$this->prefix."_user_attributes (
				ID INTEGER,
				name TEXT,
				value TEXT
			)",
			"drop table ".$this->prefix."_sessions",
			"CREATE TABLE ".$this->prefix."_sessions (
				user INTEGER,
				hash TEXT,
				date DATETIME
			)",
			"drop table ".$this->prefix."_session_attributes",
			"CREATE TABLE ".$this->prefix."_session_attributes (
				ID INTEGER,
				name TEXT,
				value TEXT
			)",
			"drop table ".$this->prefix."_messages",
			"CREATE TABLE ".$this->prefix."_messages (
				ID INTEGER AUTO_INCREMENT,
				PRIMARY KEY (ID),
				msg_from TEXT,
				msg_to INTEGER,
				submitedBy TEXT,
				title TEXT,
				message TEXT,
				date DATETIME
			)",
			"drop table ".$this->prefix."_keys",
			"CREATE TABLE ".$this->prefix."_keys (
				ID INTEGER AUTO_INCREMENT,
				PRIMARY KEY (ID),
				key_to INTEGER,
				key TEXT,
				date DATETIME
			)",
			""
		);

		$i = 0;
		foreach ($SQLStrings as $SQLQuery) {
			$Insrt = @mysql_query($SQLQuery);
			if ($this->dbg) if ($Insrt) echo "OK: ";
			if ($this->dbg) if (!$Insrt) echo "--- FEHLER:: ";
			if ($this->dbg) echo $SQLQuery."<br>";
		}
	} 
	function query($command, $table, $options) {
		if ($this->dbg) echo "<small>".$command." ".$this->prefix."_".$table." ".$options."</small><br>";
		return mysql_query($command." ".$this->prefix."_".$table." ".$options."");
	}
	function num_rows($resource) {
		return mysql_num_rows($resource);
	}
	function fetch_Array($resource) {
		return mysql_fetch_Array($resource);
	}
	function queryToArray($command, $table, $options, $ids_as_indizes = 0) {
		if ($this->dbg) echo "<small>".$command." ".$this->prefix."_".$table." ".$options."</small><br>";
		$result = mysql_query($command." ".$this->prefix."_".$table." ".$options."");
		if ($this->debug == 1)
			echo "\n\n".$command." ".$this->prefix."_".$table." ".$options."\n\n";
		if ($result) {
			for ($i=0; $i < mysql_num_rows($result); $i++){
				if ($ids_as_indizes == 1) {
					$f = mysql_fetch_Array($result);
					if (isset($f["ID"])) {
						$field[$f["ID"]] = $f;
					} else {
						$field[] = $f;
					}
				} else {
					$field[] = mysql_fetch_Array($result);
				}
			}
			return $field;
		} else return -1;
	}
	function queryToAssocArray($command, $table, $options, $name = "name") {
		if ($this->dbg) echo "<small>".$command." ".$this->prefix."_".$table." ".$options."</small><br>";
		$result = mysql_query($command." ".$this->prefix."_".$table." ".$options."");
		if ($this->debug == 1)
			echo "\n\n".$command." ".$this->prefix."_".$table." ".$options."\n\n";
		if ($result) {
			for ($i=0; $i < mysql_num_rows($result); $i++){
				$f = mysql_fetch_Array($result);
				if (isset($f[$name])) {
					$field[$f[$name]] = $f;
				} else {
					$field[] = $f;
				}
			}
			return $field;
		} else return -1;
	}
	function queryIDs($command, $table, $options, $name = "ID") {
		if ($this->dbg) echo "<small>".$command." ".$this->prefix."_".$table." ".$options."</small><br>";
		$result = mysql_query($command." ".$this->prefix."_".$table." ".$options."");
		if ($this->debug == 1)
			echo "\n\n".$command." ".$this->prefix."_".$table." ".$options."\n\n";
		if ($result) {
			for ($i=0; $i < mysql_num_rows($result); $i++){
				$f = mysql_fetch_Array($result);
				$field[] = $f[$name];
			}
			return $field;
		} else return -1;
	}
}

$db = new DB();

?>